#ifndef COLOR_H
#define COLOR_H

#include <ctype.h>
#include <string.h>

FILE* file = 0;

int set_color(int color)
{
    float c;
    c = (float)color/255.0f;
    glColor3f(c, c, c);  
    return 1;
}

typedef struct RANGE Range;

struct RANGE {
    int end_color;
    int delta_color;
    float mark;
};

Range ranges[100];
int ranges_count;

int split_string(const char* str)
{
    const char* ch;    
    char buffer[8];
    int chcount;
    int b_first;
    int b_num;
    int i;
    ranges_count = 0;
    chcount = 0;
    b_first = 1;
    b_num = 0;
    for(ch = &str[0]; *ch!='\0'; ++ch) {
        if(isdigit(*ch)) {
            buffer[chcount++] = *ch;
            b_num = 1;
        }  else {   
            if(!b_num)
                continue; 
            b_num = 0;
            buffer[chcount]='\0';
            if(b_first) {
                b_first = 0;
                ranges[ranges_count].end_color = 
                    atoi(buffer);
            } else {
                b_first = 1;
                ranges[ranges_count].delta_color = 
                    atoi(buffer);
                ++ranges_count;
            }
            chcount = 0;
        }
    }
    for(i = 0; i < ranges_count; ++i) {
        printf("end color: %d, delta color: %d\n", 
            ranges[i].end_color, ranges[i].delta_color);
    }
    return 1;
}

int load_table()
{
    char buffer[10000];
    char* str;
    int size;
    file = fopen("color_table.txt", "r");
    size = fread(buffer, 1, 10000, file);
    fclose(file);
    file = 0;
    printf("File size %d\n", size);
    str = malloc(size+1);
    memcpy(str, buffer, size);
    str[size]='\0';
    printf("%s\n", str);
    split_string(str);
    return 1;
}

int pixels[256];
float total_mark;

int make_marks()
{
    int max;
    int i;
    int pi;
    int b_done;
    float mark;
    int u;
    max = 0;
    
    for(i = 0; i < ranges_count; ++i)
    {
        if(ranges[i].delta_color > max) {
            max = ranges[i].delta_color;
        }
    }
    for(i = 0; i < ranges_count; ++i)
    {
        ranges[i].mark = (float)max / 
            (float)ranges[i].delta_color;   
    }
    for(pi=0; pi<=255; ++pi) 
    {
        b_done = 0;
        for(i = 0; i < ranges_count; ++i)
        {
            if(ranges[i].end_color < pi)
                continue;
            pixels[pi]=ranges[i].mark;
            b_done = 1;
            break;
        }
        if(!b_done) {
            mark = pixels[pi-1];
            for(u=pi; u <=255; ++u) 
            {
                pixels[u]=mark;
            }
            break;
        }
    }
    total_mark = 0;
    for(u=0; u <=255; ++u)
    {
        total_mark += pixels[u];
        printf("%d: %d, ", u, (int)pixels[u]);
    }
    printf("\nTotal: %d\n", (int)total_mark);
    return 1;
}

int colors[100];

int make_color_range(int steps_count, int* colors)
{
    float mps;
    int i;
    int marks;
    int p;
    mps = total_mark / steps_count;
    p = 0;
    for(i = 0; i < steps_count; ++i)
    {
        marks = 0;
        while(p<=255)
        {
            marks+=pixels[p++];
            if(marks>=mps)
                break;
        }
        colors[i]=p-1;
        printf("%d ", colors[i]);
    }
    printf("\n");
    return 1;
}

float calc_slice_total_mark(int minc, int maxc)
{
    float total;
    int i;
    total = 0;
    for(i = minc; i<=maxc; ++i)
    {
        total+=pixels[i];    
    }
    return total;
}

int make_slice_color_range(int steps_count, int* colors, 
    int minc, int maxc)
{
    float mps;
    int i;
    int marks;
    int p;
    float total;
    total = calc_slice_total_mark(minc, maxc);
    mps = total / steps_count;
    p = minc;
    for(i = 0; i < steps_count; ++i)
    {
        marks = 0;
        while(p<=255)
        {
            marks+=pixels[p++];
            if(marks>=mps)
                break;
        }
        colors[i]=p-1;
        printf("%d ", colors[i]);
    }
    printf("\n");
    return 1;
}

#endif
